<?php
session_start();
require_once 'conn.php'; // 数据库连接

// 判断是否登录
if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => '请先登录']);
    exit;
}

// 获取数据
$data = json_decode(file_get_contents('php://input'), true);
$cart_id = isset($data['cart_id']) ? intval($data['cart_id']) : 0;
$user_id = $_SESSION['user_id']; // 从 session 获取用户 id

if ($cart_id > 0) {
    // 删除购物车商品
    $sql = "DELETE FROM cart WHERE id = $cart_id AND user_id = $user_id";
    if (mysqli_query($conn, $sql)) {
        echo json_encode(['success' => true]);
    } else {
        echo json_encode(['success' => false, 'message' => '删除失败']);
    }
} else {
    echo json_encode(['success' => false, 'message' => '无效的购物车项']);
}

mysqli_close($conn);
?>
